/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */
package cn.webrx.interceptor;

import cn.webrx.utils.SnowFlakeUtils;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * <p></p>
 * <p>Powered by webrx On 2023-04-20 17:56:39</p>
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */

@Intercepts({
        @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})
})
public class MyPager implements Interceptor {

    SnowFlakeUtils su;

    public void setSu(SnowFlakeUtils su){
        this.su=su;
    }

    public static void startPage(int c, int p) {
        currpage = c;
        pagesize = p;
    }

    public static void startPage(int c) {
        currpage = c;
    }

    public static int currpage = 1;
    public static int pagesize = 10;
    public static int recordcount = 0;
    public static int pagecount = 1;

    public static String pageInfo() {
        return String.format("[第 %d 页/共 %d 页，每页 %d 条/共 %d 条]", currpage, pagecount, pagesize, recordcount);
    }


    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object target = invocation.getTarget();
        MetaObject metaObject = SystemMetaObject.forObject(target);
        MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
        if (mappedStatement.getSqlCommandType() == SqlCommandType.SELECT && mappedStatement.getId().endsWith("ByPage")) {

            String sql = metaObject.getValue("delegate.boundSql.sql").toString();
            //System.out.println(sql.trim());

            System.out.println(su.nextId());
            System.out.println(su.nextId());
            System.out.println(su.nextId());

            String sql1 = "select count(*) " + sql.substring(sql.indexOf("from"));
            Connection connection = (Connection) invocation.getArgs()[0];

            try {
                PreparedStatement pst = connection.prepareStatement(sql1);
                ResultSet rs = pst.executeQuery();
                rs.next();
                recordcount = rs.getInt(1);
            } catch (Exception e) {
                e.printStackTrace();
            }
            pagecount = recordcount % pagesize == 0 ? recordcount / pagesize : recordcount / pagesize + 1;


            String sql2 = sql.trim().concat(String.format(" limit %d,%d", currpage * pagesize - pagesize, pagesize));
            metaObject.setValue("delegate.boundSql.sql", sql2);



            //metaObject.getValue("delegate.boundSql.sql") 获取sql
            //System.out.println(metaObject.getValue("delegate.boundSql.sql"));

            //设置参数
            //ParameterHandler parameterHandler = (ParameterHandler)metaObject.getValue("delegate.parameterHandler");
            //Object param = parameterHandler.getParameterObject();
            //Book book = (Book)param;
            //System.out.println(book);
            //book.setPrice(100);
            //System.out.println(book);

            //设置sql语句
            //metaObject.setValue("delegate.boundSql.sql","insert absdfasdf....");


        }


        Object obj = invocation.proceed();
        return obj;
    }
}
